package ru.ruappmaker.calendar;

import java.util.ArrayList;
import java.util.List;

import com.flurry.android.FlurryAgent;
import com.google.ads.AdRequest;
import com.google.ads.AdView;
import com.viewpagerindicator.TabPageIndicator;
import ru.ruappmaker.calendar.R;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MonthView extends CalendarActivity {

	private class MonthPrintTask extends AsyncTask<Integer, Void, List<View>> {
		@Override
		protected List<View> doInBackground(Integer... arg0) {
			List<View> views = new ArrayList<View>();
			for (int i = 0; i < 12; i++) {
				View monthView = inflater.inflate(R.layout.month_view, null);
				FillMonth(monthView, i);
				views.add(monthView);
			}
			return views;
		}

		@Override
		protected void onPostExecute(List<View> views) {
			viewPager.setAdapter(new CalendarPager(views));
			pageIndicator.notifyDataSetChanged();
			pageIndicator.setViewPager(viewPager);
			pageIndicator.setCurrentItem(selectedMonth);
			pageIndicator.setVisibility(View.VISIBLE);
		}
	}

	public ViewPager viewPager;
	public TabPageIndicator pageIndicator;
	private String[] months, ofMonths;
	private int selectedMonth;
	public LayoutInflater inflater;
	public CalendarPager pagerAdapter;

	private OnPageChangeListener pageListener = new OnPageChangeListener() {
		public void onPageSelected(int arg0) {
			selectedMonth = arg0;
			getSupportActionBar().setTitle(
					months[arg0] + " " + String.valueOf(year()));
			FlurryAgent.logEvent("Month view: month changed to " + arg0);
		}

		public void onPageScrollStateChanged(int arg0) {
		}

		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}
	};

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Intent instance = getIntent();
		Bundle extras = instance.getExtras();
		months = getResources().getStringArray(R.array.monthNames);
		ofMonths = getResources().getStringArray(R.array.ofMonthNames);
		inflater = LayoutInflater.from(this);

		if (extras != null) {
			_year = extras.getInt("year");
			selectedMonth = extras.getInt("month") - 1;
		}
		InitViewPager();
	}

	@Override
	protected void InitViewPager() {
		FillDataArrays();
		List<View> pages = new ArrayList<View>();
		View month = inflater.inflate(R.layout.month_view, null);
		FillMonth(month, selectedMonth);
		pages.add(month);

		setContentView(R.layout.pager_layout);
		pagerAdapter = new CalendarPager(pages);
		
		viewPager = (ViewPager)findViewById(R.id.pager);
		viewPager.setAdapter(pagerAdapter);
		viewPager.setPageMargin(1);
		viewPager.setPageMarginDrawable(R.color.screen_split);
		pageIndicator = (TabPageIndicator) findViewById(R.id.page_indicator);
		pageIndicator.setOnPageChangeListener(pageListener);
		pageIndicator.setViewPager(viewPager);
		pageIndicator.setCurrentItem(selectedMonth);
		pageIndicator.setVisibility(View.INVISIBLE);
		MonthPrintTask task = new MonthPrintTask();
		getSupportActionBar().setTitle(
				months[selectedMonth] + " " + String.valueOf(year()));

		task.execute(selectedMonth);
		 AdView adView = (AdView)this.findViewById(R.id.adView);
		    AdRequest adRequest = new AdRequest();
		    adView.loadAd(adRequest);
	}

	private void FillMonth(View month, int number) {
		View monthContainer = month.findViewById(R.id.month_container);
		LinearLayout calTable = (LinearLayout) monthContainer
				.findViewById(R.id.month_calendar);

		createCalendar(calTable, number + 1, year(), true);
		((TextView) month.findViewById(R.id.calendar_days)).setText(String
				.valueOf(monthInfo.get(number).calDays));
		((TextView) month.findViewById(R.id.work_days)).setText(String
				.valueOf(monthInfo.get(number).workDays));
		((TextView) month.findViewById(R.id.holiday_days)).setText(String
				.valueOf(monthInfo.get(number).holiDays));
		((TextView) month.findViewById(R.id.work_hours_40)).setText(String
				.valueOf(monthInfo.get(number).work40));
		((TextView) month.findViewById(R.id.work_hours_36)).setText(String
				.valueOf(monthInfo.get(number).work36));
		((TextView) month.findViewById(R.id.work_hours_24)).setText(String
				.valueOf(monthInfo.get(number).work24));
		
		TextView sdView = (TextView) month.findViewById(R.id.shortDays);
		String shortDaysLabel = "";
		if (shortDays[number] != null) { 
			int dayslen = shortDays[number].size();
			shortDaysLabel = "Короткие дни: ";
			for (int i = 0; i < dayslen; i++) 
				shortDaysLabel += shortDays[number].get(i).toString() + " " + ofMonths[number] + (i == dayslen-1 ? "." : ", ");
		}
		sdView.setText(shortDaysLabel);
		sdView.setTextColor(getColorByAttribute(R.attr.themeColor));
		
	}
	
	@Override
	protected void RestartThis() {
		Intent i = new Intent(this, getSubClassObject().getClass());
		i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		i.putExtra("month", selectedMonth + 1);
		i.putExtra("year", year());
		this.finish();
		this.startActivity(i);	
	}
}
